#include "video_base.h"

using namespace  video;



int32_t frame_add_ref( void *frame ){
    IMemory *pMem = (IMemory *)frame;
    pMem->add_ref();
}

int32_t frame_dec_ref( void *frame ){
    IMemory *pMem = (IMemory *)frame;
    pMem->release();
}


int32_t memory_base_create_fq(  int32_t w, int32_t h, int32_t w_stride, int32_t h_stride, FRAME_TYPE type, int32_t num, MEM_BASE_HANDLE *handle ){

    int32_t solt_id;
    int32_t ret = 0;

    MemoryFrameIns *pIns = new MemoryFrameIns();
    if( pIns == NULL ){
        return -1;
    }

    ret = IMemoryPool::getInstance()->createSolt(&solt_id, w, h, w_stride, h_stride, type, num);
    if( ret != 0 ){
        return -1;
    }
    pIns->w = w;
    pIns->h = h;
    pIns->w_stride = w_stride;
    pIns->h_stride = h_stride;
    pIns->solt_id = solt_id;
    pIns->buf_size = 0;
    pIns->type = type;
    pIns->max_num = num;

    *handle = pIns;
    return 0;
}


int32_t memory_base_fq_acquire( MEM_BASE_HANDLE handle, struct MemoryFrame *frame ){

    MemoryFrameIns *pIns = (MemoryFrameIns *)handle;

    if( pIns == NULL ){
        return -1;
    }

    IMemory *pMem = IMemoryPool::getInstance()->get(pIns->solt_id);
    if( pMem == NULL ){
        return -1;
    }
    Video_Frame *pAMem = static_cast<Video_Frame*>(pMem);
    if( pMem == NULL ){
        return -1;
    }
    frame->frame = pAMem;
    frame->addr = pAMem->memory();
    frame->size = pAMem->size();
    frame->type = pAMem->type();
    frame->w = pIns->w;
    frame->h = pIns->h;
    frame->w_stride = pIns->w_stride;
    frame->h_stride = pIns->h_stride;
    frame->timestamp = pAMem->timestamp();
    frame->ops.add_ref = frame_add_ref;
    frame->ops.dec_ref = frame_dec_ref;
    return 0;
}


int32_t memory_base_destory_fq( MEM_BASE_HANDLE handle ){

    if( handle == NULL ){
        return -1;
    }

    MemoryFrameIns *pIns = (MemoryFrameIns *)handle;

    IMemoryPool::getInstance()->destorySolt(pIns->solt_id);

    delete pIns;
    pIns = NULL;

    return 0;
}